//
//  AnnouncementViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/25.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "AnnouncementViewController.h"
#import "AnnouncementCell.h"
#import "AnnouncementApi.h"
#import "AnnouncementShowViewController.h"

@interface AnnouncementViewController ()

@end

@implementation AnnouncementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"公告";
    [self loadData];
    [self.tableView registerCellWithNib:@"AnnouncementCell"];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadData
{
    NSDate *date = [NSDate date];
    
    NSTimeInterval lastTime = [[Config userDefaultsGetObjectForKey:kAnnouncementTimestamp] doubleValue];
    WeakSelf(self)
    AnnouncementApi *api = [[AnnouncementApi alloc]initWithTimeInterval:lastTime];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        if (kCode(request.responseJSONObject)) {
            [weakself.dataArray removeAllObjects];
            
            for (NSDictionary *dic in [request.responseJSONObject objectForKey:@"data"]) {
                Announcement *obj = [Announcement new];
                [obj setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:obj];
                [Config userDefaultsSetObject:@(date.timeIntervalSince1970) key:kAnnouncementTimestamp];
            }
            
            [weakself.tableView reloadData];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnnouncementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnnouncementCell" forIndexPath:indexPath];
    Announcement *obj = self.dataArray[indexPath.row];
    cell.time.text = obj.created_at;
    [cell setTitle:obj.title text:obj.body];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Announcement *obj = self.dataArray[indexPath.row];
    AnnouncementShowViewController *vc = [[AnnouncementShowViewController alloc]init];
    vc.view.backgroundColor = [UIColor whiteColor];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:obj.title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    [str appendAttributedString:[[NSAttributedString alloc]initWithString:@"\n"]];
    [str appendAttributedString:[[NSAttributedString alloc]initWithString:@"\n"]];
    [str appendAttributedString:[[NSMutableAttributedString alloc]initWithString:obj.body attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}]];
    
    NSMutableParagraphStyle *centerStyle = [[NSMutableParagraphStyle alloc] init];
    [centerStyle setAlignment:(NSTextAlignmentCenter)];
    [str addAttribute:NSParagraphStyleAttributeName value:centerStyle range:NSMakeRange(0, obj.title.length)];
    
    vc.textView.attributedText = str;
    vc.title = obj.title;
    [self pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
